package com.bbzn.device.client.service.emq.alarm;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.TitleConstant;
import com.bbzn.device.client.dataobject.Switch;
import com.bbzn.device.client.dto.ct.data.EmqAlarmDTO;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.SwitchService;
import com.bbzn.device.client.service.emq.alarm.EmqBaseAlarm;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2021/12/3 16:04
 * @Version 1.0
 */
@Component
public class EmqHarmfulGasAlarmStrategy extends EmqBaseAlarm implements EmqStrategy {

    @Resource
    private SwitchService switchService;

    @Override
    public String emqAction(String payLoad) {
        EmqAlarmDTO emqAlarm = new EmqAlarmDTO();
        List<String> list = new ArrayList<>();
        list.add("imei");
        list.add("info");
        list.add("state");
        list.add("type");
        list.add("lat");
        list.add("lon");
        list.add("location");

        /*Long userId = this.checkPayload(list, payLoad);
        if (userId == null) {
            return "";
        }*/

        JSONObject jsonObject = JSONObject.parseObject(payLoad);
        //0：气体正常  ，1：发生报警
        Integer state = jsonObject.getInteger("state");
        String cnInfo = null;
        String enInfo = null;
        if (state == 0) {  //0：正常  1：预警
            cnInfo = "正常消息: 没有检测到有害气体";
            enInfo = "normal message: no harmful gas is detected";
        } else {
            cnInfo = "异常消息: 检测到有害气体";
            enInfo = "abnormal message: harmful gas detected";
        }
        // 定位类型【1：GPS / 2：传感器 / 4：缓存定位 / 5：wifi定位 / 6：基站定位】
        Integer type = jsonObject.getInteger("type");
        if (null == type || "".equals(type)){
            type = 5;
        }
        String lat = jsonObject.getString("lat");
        String lon = jsonObject.getString("lon");
        String location = jsonObject.getString("location");

        emqAlarm.setImei(jsonObject.getString("imei"));
        emqAlarm.setAlarmType(17);
        emqAlarm.setTitle(TitleConstant.HARMFUL_GAS_ALARM);
        emqAlarm.setEnTitle(TitleConstant.HARMFUL_GAS_ALARM_EN);

        String cnSign = MessageFormat.format("[有害气体报警]-{0}", cnInfo);
        String enSign = MessageFormat.format("[harmful gas alarm]-{0}", enInfo);
        emqAlarm.setCnSign(cnSign);
        emqAlarm.setEnSign(enSign);
        emqAlarm.setLat(lat);
        emqAlarm.setLon(lon);
        emqAlarm.setLocation(location);
        emqAlarm.setType(type);

        // web端有害气体/短信/邮箱开关检查
        Switch need = new Switch();
        need.setImei(jsonObject.getString("imei"));
        List<Boolean> checkResult = switchService.checkSwitchByHarmfulGas(need);
        emqAlarm.setBooleanList(checkResult);
        this.alarmRun(emqAlarm);
        return "";
    }
}
